package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtDraw;
import com.greyu.ysj.entity.CbtDrawExample;
import com.greyu.ysj.mapper.CbtDrawMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtDrawService;

@Service
@Transactional
public class CbtDrawServiceImpl implements CbtDrawService {
	@Autowired
	private CbtDrawMapper cbtDrawMapper;


	@Override
	public ResultModel save(CbtDraw cbtDraw) {
		try {	
			if ( cbtDraw.getAccount()==null
					||cbtDraw.getAmount()==null
					||cbtDraw.getDrawDate()==null
					||cbtDraw.getUserId()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtDrawMapper.insert(cbtDraw);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.DRAW_INSERT_ERROR);
		}
	        return ResultModel.ok(cbtDraw);
		}

	@Override
	public List<CbtDraw> selectDrawRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtDrawExample cbtDrawExample=new CbtDrawExample();
		CbtDrawExample.Criteria criteria=cbtDrawExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		cbtDrawExample.setOrderByClause("`Id` DESC");
		List<CbtDraw> list=cbtDrawMapper.selectByExample(cbtDrawExample);
		
		if(list==null || list.size()<1){
			
			return null;
		}
		return list;
	}


	@Override
	public int totalPages(Integer userId, Integer rows) {
		CbtDrawExample cbtDrawExample=new CbtDrawExample();
		CbtDrawExample.Criteria criteria=cbtDrawExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		long total=cbtDrawMapper.countByExample(cbtDrawExample);
		int totalPages;
		if(total%rows==0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}

	@Override
	public int selectMaxDrawId() {
		int drawId=this.cbtDrawMapper.selectMaxDrawId();
		return drawId;
	}

	@Override
	public CbtDraw selectCbtDrawById(Integer id) {
		return this.cbtDrawMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResultModel updateCbtDraw(Integer id, CbtDraw cbtDraw) {
		CbtDraw newDraw=this.cbtDrawMapper.selectByPrimaryKey(id);
		if(newDraw == null) {
			 return ResultModel.error(ResultStatus.DRAW_NOT_FOUND);
		}
		if(cbtDraw.getStatus() != null){
			newDraw.setStatus(cbtDraw.getStatus());
		}
		 this.cbtDrawMapper.updateByPrimaryKey(newDraw);
		return ResultModel.ok(newDraw);
	}

	@Override
	public boolean isHashExist(String hash) {
		CbtDrawExample cbtDrawExample = new CbtDrawExample();
		CbtDrawExample.Criteria criteria = cbtDrawExample.createCriteria();
		criteria.andDrawHashEqualTo(hash);
        List<CbtDraw> list = this.cbtDrawMapper.selectByExample(cbtDrawExample);
        if(list==null||list.size()<1) {
        	return false;
        }else {
        	
        	return true;
        }
	}

	@Override
	public List<CbtDraw> selectAll(CbtDraw cbtDraw) {
		CbtDrawExample cbtDrawExample = new CbtDrawExample();
		CbtDrawExample.Criteria criteria = cbtDrawExample.createCriteria();
		criteria.andStatusEqualTo(cbtDraw.getStatus());
        List<CbtDraw> list = this.cbtDrawMapper.selectByExample(cbtDrawExample);
        return list;
	}

	@Override
	public double selectTotalDrawByuserId(int userId) {
		double totalDraw=this.cbtDrawMapper.selectTotalDrawByuserId(userId);
		return totalDraw;
	}

	@Override
	public int countTodayByUserId(Integer userId) {
		// TODO Auto-generated method stub
		int countTodayByUserId = this.cbtDrawMapper.countTodayByUserId(userId);
		return countTodayByUserId;
	}

	@Override
	public List<CbtDraw> selectAllDrawByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return this.cbtDrawMapper.selectAllDrawByuserId(userId);
	}


}




